<?php
/**
 * 
 * A controller class for game control, all game zone, center, question generate all in here.
 * @author Seelove00
 *
 */
class PlayController extends Controller
{
  public $defaultAction = 'center';
  
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('center', 'getEquation', 'saveEquation'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
    
	public function actionCenter()
	{
		$this->render('center');
	}
	
	
    
	/**
	 * 
	 * get equation via ajax
	 */
	public function actionGetEquation()
	{
	    if (isset($_POST['Equation'])){
	        
        //get map info, especially times
        $mapInfo = Map::model()->findByAttributes(array('code'=>$_POST['Equation']['code']));
        
        $roundTimes = (int)$mapInfo->times; //get all times
        $roundValue = (int)$mapInfo->value;//get value for every question
        
        //add map log started info
        $newMapLog = new MapLog();
        $newMapLog->map_id = $mapInfo->id;
        $newMapLog->uid = Yii::app()->user->id;
        $newMapLog->start_date = date('Y-m-d H:i:s');
        $newMapLog->save(); 
        
        Yii::app()->session['roundMapLogId'] = $newMapLog->id;
        Yii::app()->session['roundValue'] = $roundValue;
        
        $generateMath = array();
        //set value
        $generateMath['value'] = $roundValue;
        $generateMath['times'] = $roundTimes;
        
        
        //set offset for Reequation Generate usage
        $offSetRe = 0;
        
        $criteria = new CDbCriteria();
    	  $criteria->condition = ' uid = :uid and end_date is null ';
    	  $criteria->params[':uid'] = Yii::app()->user->id;
        
        
        if ($_POST['Equation']['code']=='L061'){
            //get today wrong equation number
            
            $criteria->condition .= ' and add_date > :today_date ';
	          $criteria->params[':today_date'] = date('Y-m-d');
            
            $offSetRe = (int)Reequation::model()->count($criteria);
        }else {
            if ($_POST['Equation']['code']=='L062'){
                //get all wrong equation number
                $offSetRe = (int)Reequation::model()->count($criteria);    
            }
        }
        
//        var_dump($offSetRe);
        
        for ($i=0; $i<$roundTimes; $i++)
            $generateMath['e'][$i] = Equation::model()->generateMath($_POST['Equation']['code'], $offSetRe);

//        $generateMath['e'] = Equation::model()->generateMath($_POST['Equation']['code'], $offSetRe);
	            
        echo json_encode($generateMath);
	        
	    }
	}
	
	
	/**
	 * save it.
	 */
	public function actionSaveEquation()
	{
	    if (isset($_POST['Equation'])) {
	        $newEquation = new Equation('Create');
	        $newEquation->first = $_POST['Equation']['F'];
	        $newEquation->operator = $_POST['Equation']['O'];
	        $newEquation->second = $_POST['Equation']['S'];
	        $newEquation->answer = $_POST['Equation']['A'];
	        $newEquation->left = $_POST['Equation']['L'];
	        $newEquation->map_type_code = $_POST['Equation']['typeCode'];
	        
	        $rightAnswer = $newEquation->getEquationAnswer();
	        
	        $currentUser = User::model()->findByPk(Yii::app()->user->id);
	        
	        if ($newEquation->answer == $rightAnswer['A']&&$newEquation->left==$rightAnswer['L']){
	            $newEquation->tof = 1;
	            $arrReturn = array('tof'=>1);
	            
//	            var_dump(Yii::app()->session['roundValue']);
	            //money earned
//	            var_dump($currentUser->money);
	            $currentUser->money += (int)Yii::app()->session['roundValue'];
//	            var_dump($currentUser->money);
	            //experience add and check level upgrade
	            //TODO
	            
	            $currentUser->save();
	            
	        }else {
	            $newEquation->tof = 0;
	            $arrReturn = array('tof'=>0);
	            //money lost
	            $currentUser->money -= (int)Yii::app()->session['roundValue']*2;
//	            var_dump(Yii::app()->session['roundValue']);
	            $currentUser->save();
	        }
	        
	        if ($_POST['MapEnd']){
	            //add map log finished info
	            $currentMapLog = MapLog::model()->findByPk(Yii::app()->session['roundMapLogId']);
	            $currentMapLog->complete_date = date('Y-m-d H:i:s');
	            $currentMapLog->save();
	            
	            unset(Yii::app()->session['roundMapLogId']);
	        }
	        
	        if ($newEquation->tof == 1){
	            //if right check Reequation or Equation
	            if ($_POST['Equation']['id'] != 0 ){
	                //right in Re Mode
	                $currReEquation = Reequation::model()->findByPk($_POST['Equation']['id']);
	                $currReEquation->right_time += 1;
	                if ($currReEquation->right_time >= 2){
	                    //add complete info
	                    $currReEquation->end_date = date('Y-m-d H:i:s');
	                }
	                $currReEquation->save();
	                $newEquation->save();
	                
	            }else{
	                //right in Normal Mode
    	            $newEquation->save();
	            }
	        }else{
	            //if wrong check Reequation or Equation
	            if ($_POST['Equation']['id'] != 0 ){
	                //wrong in Re Mode
	                $currReEquation = Reequation::model()->findByPk($_POST['Equation']['id']);
	                $currReEquation->right_time -= 1;
	                $currReEquation->save();
	                $newEquation->save();
	            }else{
	                //wrong in Normal Mode
    	            //record Reequtaion
    	            $newReEquation = new Reequation();
    	            $newReEquation->uid = Yii::app()->user->id;
    	            $newReEquation->first = $newEquation->first;
    	            $newReEquation->operator = $newEquation->operator;
    	            $newReEquation->second = $newEquation->second;
    	            $newReEquation->add_date = date('Y-m-d H:i:s');
    	            $newReEquation->map_type_code = $_POST['Equation']['typeCode'];
    	            
    	            $newReEquation->save();
	                
    	            $newEquation->save();
	            }
	        }
	        
	        echo json_encode($arrReturn);
	        
	        
	        
	    }
	}
	
	
	
	
}